<?php
declare (strict_types = 1);

namespace app\controller\organization;

use app\BaseController;
use app\model\OrganizationMembers;
use think\Request;
use think\Response;

class Member extends BaseController
{
    private function model(): OrganizationMembers
    {
        return new OrganizationMembers();
    }

    /**
     * 查询组织成员
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $oid = $request->param('oid');
        try {
            $data = $this->model()::with(['user', 'role'])
                ->where('oid', $oid)
                ->select()
                ->map(function ($item) {
                    return [
                        'id' => $item->id,
                        'user_id' => $item->user->id,
                        'username' => $item->user->username,
                        'mobile' => $item->user->mobile,
                        'email' => $item->user->email,
                        'avatar' => $item->user->avatar,
                        'role_id' => $item->role->id,
                        'role_name' => $item->role->name,
                        'role_color' => $item->role->color,
                        'role_slug' => $item->role->slug,
                        'join_time' => $item->join_at,
                        'status' => $item->status,
                    ];
                })->toArray();
            return  apiResponse(200, '成功', $data);
        }catch (\Exception $e){
            return apiResponse(500, '服务器错误'.$e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return Response
     */
    public function delete($id)
    {
        //
    }
}
